/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link class="back" :to="{name:'sms_tpl'}" title="返回列表">
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>短信配置模板 - 编辑模板</h3>
            <h5>网站系统短信配置模板管理</h5>
          </div>
        </div>
      </div>
      <form :model="form">
        <div class="ncap-form-default">
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>短信签名
              </label>
            </dt>
            <dd class="opt">
              <input v-model="form.sms_sign" type="text" class="input-txt" />
              <p class="err-msg" v-if="errResult.sms_sign && !form.sms_sign">{{errResult.sms_sign}}</p>
              <p class="notic">必须与短信平台短信签名一致</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>短信模板ID
              </label>
            </dt>
            <dd class="opt">
              <input v-model="form.sms_tpl_code" type="text" class="input-txt" />
              <p
                class="err-msg"
                v-if="errResult.sms_tpl_code && !form.sms_tpl_code"
              >{{errResult.sms_tpl_code}}</p>
              <p class="notic">必须与短信平台短信模板ID一致</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>发送场景
              </label>
            </dt>
            <dd class="opt">
              <span v-if="this.id != null">{{form.send_scene_detail}}</span>
              <el-select
                v-if="this.id == null"
                size="small"
                v-model="form.send_scene"
                placeholder="请选择使用场景"
              >
                <el-option
                  v-for="item in sendScene"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
              <p
                class="err-msg"
                v-if="errResult.send_scene && !form.send_scene"
              >{{errResult.send_scene}}</p>
              <p class="notic">使用场景</p>
            </dd>
          </dl>
          <dl class="row">
            <dt class="tit">
              <label>
                <em>*</em>短信内容
              </label>
            </dt>
            <dd class="opt">
              <textarea
                v-model="form.tpl_content"
                placeholder
                name="description"
                rows="6"
                class="tarea"
              ></textarea>
              <p
                class="err-msg"
                v-if="errResult.tpl_content && !form.tpl_content"
              >{{errResult.tpl_content}}</p>
            </dd>
          </dl>
          <div class="bot">
            <el-button type="primary" @click="onSubmit()" :loading="isLoading">确认提交</el-button>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {},
      sendScene: [],
      isLoading: false,
      errResult: {}
    };
  },
  components: {},
  created() {
    this.getSendScene();
    if (typeof this.$route.params.id != "undefined") {
      //编辑
      this.id = this.$route.params.id;
      this.getSmsTpl();
    }
  },
  methods: {
    getSmsTpl() {
      var that = this;
      this.request
        .get("user/sms_template?tpl_id=" + this.id)
        .then(function(res) {
          that.form = res;
        });
    },
    getSendScene() {
      var that = this;
      this.request.get("user/sms_template/send_scene").then(function(res) {
        that.sendScene = res;
      });
    },
    onSubmit() {
      this.isLoading = true;
      var that = this;
      if (this.id != null) {
        //编辑
        this.request.put("/user/sms_template", this.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("保存成功");
            that.$router.push({ name: "sms_tpl" });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        this.request.post("/user/sms_template", this.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success("添加成功");
            that.$router.push({ name: "sms_tpl" });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
</style>
